<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Cert extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
        $this->load->model('Cert_model');
    }
	

	public function index()
	{
		$userid = $this->thatUser['id'];
		$that_usertype = $this->thatUser['usertype'];
		if ($this->input->is_post())
		{
		    //申请认证可获得200魔币
		    $this->load->model('Cert_model');
		    $certModel = $this->Cert_model->get_by_id($userid);
		    if (!$certModel)
		    {
	            $this->load->service('magic_service');
	            $this->magic_service->recordMagicCoins($userid,200,3);
	            
	            //邀请的新用户认证可获得300魔币
	            $this->load->model('User_recommed_model');
	            $userRecommedModel = $this->User_recommed_model->get_by_where(array('recom_uid'=>$userid));
	            if ($userRecommedModel)
	            {
	                $this->magic_service->recordMagicCoins($userRecommedModel['uid'],300,8);
	            }
		    }
		    
			$res = $this->save();
			redirect('/m/cert?res='.$res);
			exit;
		}

		$o = $this->Cert_model->get_by_id($userid);
		
		//begin:右侧-推荐用户
		$right_usertype = 1;
		$right_sex = 1;
		if($this->loginUsertype == 1)
			$right_usertype = 2;
		if($this->loginUser['sex']==1)
			$right_sex = 2;
		$arrWhere = array('usertype'=>$right_usertype,'status'=>1,'userlevel'=>1,'sex'=>$right_sex);
		$feild = 'user.id,nickname,userlogo,company';
		$this->load->model('Recommend_model');
		$rightlist = $this->Recommend_model->get_user_list($arrWhere, $feild, 10);
		//end:右侧-推荐用户

		//获取子分类
		$this->load->model('userdetail_model');
		
		$oSysBail = _get_config('bail');
		$sysBail = $oSysBail[$that_usertype];
		
        // 企业或者机构类型
        $typeArr = _get_config('type', true);
        $type = $typeArr[$this->loginUsertype];
        $oSysType = array();
        if ( in_array($this->loginUsertype, array(1,2,3,6)) )
        {
            if ($this->loginUsertype == 3)
            {
                $oSysType = $this->config->item('type');
                $oSysType = $oSysType[$this->loginUsertype];
            }
            else
            {
                $oSysType = _get_config('type',true);
                $oSysType = $oSysType[$this->loginUsertype];
            }
        }
        
        // 认证完成后处理企业/机构类型字符串
        $oType = '';
        $oTypeStr = '';
        if (!empty($o) && !empty($o['com_type'])) {
            if (strstr($o['com_type'], ',')) {
                $oType = explode(',', $o['com_type']);
            } else {
                $oTypeStr = $oSysType[$o['com_type']];
            }
        }
        
        if (is_array($oType)) {
            foreach($oType as $t) {
                $oTypeStr .= $oSysType[$t].',';
            }
        }
        
		$result = array(
                    'o' => $o,
                    'sysBail' => $sysBail,
                    'rightlist' => $rightlist,
                    'usertype' => $that_usertype,
                    'oSysType' => $oSysType,
                    'oTypeStr' => rtrim($oTypeStr, ','),
                );
		$this->load->view('m/cert',$result);
	}

	private function save()
	{
           // if ($this) {
            if (in_array($this->loginUsertype, array(2, 3))) {
                // 企业和机构的认证
               $res = $this->CertOrgin($this->loginUsertype);
            } else {
                // 模特，摄影师，化妆
               $res = $this->CertPerson($this->loginUsertype);
            }
            
            return $res;
        }  
         
        // 个人认证
        private function CertPerson($type) {
            $res = 0;
            $config = array(
           	array(
                'field'   => 'realname', 
                'label'   => '真实姓名', 
                'rules'   => 'trim|required'
                ),
                array(
                    'field' => 'idno', 
                    'label' => '身份证号', 
                    'rules' => 'trim|required'
                ),
                array(
                    'field'   => 'mobile', 
                    'label'   => '手机号', 
                    'rules'   => 'trim|required'
                ),  
                array(
                    'field'   => 'idnoimg', 
                    'label'  => '身份证照片', 
                    'rules'   => 'trim|required'
                ),
                array(
                    'field' => 'email',
                    'label' => '邮箱',
                    'rules' => 'trim|required'
                )
            );

            $this->form_validation->set_rules($config);

            if ($this->form_validation->run() === TRUE) {

                $oSysKind = _get_config('orderkind');
                $oSysBail = _get_config('bail');
                $buyerid = $this->thatUser['id'];

                $o = array(
                        'userid'=>$buyerid,
                        'username'=>$this->thatUser['username'],
                        'realname'=>$this->input->post('realname'),
                        'idno'=>$this->input->post('idno'),
                        'mobile'=>$this->input->post('mobile'),
                        'idnoimg'=>$this->input->post('idnoimg'),
                        'company'=>$this->input->post('company'),
                        'email' => $this->input->post('email'), 
                        'bail'=>$oSysBail[$this->loginUsertype],
                        'addtime'=>time(),
                        'status'=>0,
                        'op_userid'=>$this->loginID,
                        'op_username'=>$this->loginUserName,
                        'op_time'=>time(),
                        'type' => $type
                    );

                //艺人
                if ($type == 6)
                {
                	// 获取艺人类型
                	$o['com_type'] = $this->input->post('type');
                }
                $this->Cert_model->insert($o);
                $res = 200;
            }

        return $res;
    }
    
    public function CertOrgin($type) 
    {
        $res = 0;
        	$config = array(
        		array(
        			'field' => 'company',
        			'label' => '名称',
        			'rules' => 'trim|required'
        		),
        		array(
        			'field' => 'com_telphone',
        			'label' => '电话',
        			'rules' => 'trim|required'
        		),
        		array(
        			'field' => 'type[]',
        			'label' => '机构类型',
        			'rules' => 'trim|required'
        		),
        		array(
        			'field' => 'idnoimg2',
        			'label' => '营业执照',
        			'rules' => 'trim|required'
        		),
        	);
        
        $this->form_validation->set_rules($config);
        if ($this->form_validation->run() === TRUE) {
            $oSysBail = _get_config('bail');
            $buyerid = $this->thatUser['id'];
            // 获取机构/企业类型
            $typeArr = $this->input->post('type');
            if (is_array($typeArr)) {
                $comType = implode(',', $typeArr); 
            } else {
                $comType = $typeArr;
            }
            
            $o = array(
                'userid'=>$buyerid,
                'username'=>$this->thatUser['username'],
                'realname'=>$this->input->post('company'),
                'idno'=>$this->input->post('idno'),
                'mobile'=>$this->input->post('com_telphone'),
                'idnoimg'=>$this->input->post('idnoimg2'),
                'company'=>$this->input->post('company'),
                'email' => $this->input->post('email'), 
                'bail'=>$oSysBail[$this->loginUsertype],
                'com_type' => $comType,
                'com_website' => $this->input->post('website'),
                'com_logo' => $this->input->post('idnoimg3'),
                'com_tel' => $this->input->post('com_telphone'),
                'com_prov' => $this->input->post('prov'),
                'com_city' => $this->input->post('city'),
                'com_dist' => $this->input->post('dist'),
                'com_address' => $this->input->post('street'),
                'addtime'=>time(),
                'status'=>0,
                'op_userid'=>$this->loginID,
                'op_username'=>$this->loginUserName,
                'op_time'=>time(),
            	'alipay' => $this->input->post('alipay'),
                'type' => $type
            );
            
            //修改分类、类型
            $data['type'] = $comType;
            $this->load->model("Userdetail_model");
            $this->Userdetail_model->update_by_where(array('userid'=>$buyerid),$data);
            
            $this->Cert_model->insert($o);
            $res = 200;
        }

        return $res;  
    }
    

	public function gotopay(){
		// $userid = $this->thatUser['id'];

		// $data = array(
		// 			'addtime'=>time(),
		// 			'status'=>2,
		// 			'op_userid'=>$this->loginID,
		// 			'op_username'=>$this->loginUserName,
		// 			'op_time'=>time(),
		// 		);
		// $this->Cert_model->update_by_id($userid, $data);

		echo '前往缴费..';
	}


}